﻿<!--// JScript File
function Invoice (gridid)
{
    this.events     = [];
    this.added      = [];
    this.index      = -1;
    this.current    = null;
    this.item       = null;
    this.gridid     = gridid;
    this.count      = 0;
    this.remove = function (id)
    {
        var result = [];
        for (var x in this.events)
        {
            if (this.events[x].ID!=id) result.push (this.events[x]); 
        }
        this.events = result;
    }
    
    this.find = function (id)
    {
        for (var x in this.events)
        {
            if (this.events[x].ID==id) return this.events[x]; 
        }
        return false;
    }
    
    this.clone = function (id, x)
    {
        var e = this.find(id);
        e.InvoiceDisplayDates=bite(e.InvoiceDisplayDates,x);
        Controller.Reload('dceivtable','dropdownmenu'); 
    }
    
    this.add = function (x)
    {
        this.added = x;
        this.count = this.added.length ;
        this.load ();
    }
    
    this.load = function ()
    {
        var item, Connector=[];
        while (this.added.length > 0)
        {
            item = new EventObject( this.added.pop() );  
            Connector.push ({ 
                'params' : { 'action' : 'range', 'id' : item.ID },
                'target' : item.CustomDates,  
                'serializer' : InvoiceCustomDateSerializer
                      });
            Connector.push ({ 
                'params' : { 'action' : 'prop', 'id' : item.ID },
                'target' : item.CustomProperties,  
                'serializer' : InvoiceCustomPropertySerializer
                      });
	        this.events.push (item);
        }
        window.connector.oncomplete = function () { Controller.Clear(); InvoicePageEvent.invoke(); };
        window.connector.send (Connector);
    }  
    window.invoice = this;
}

function InvoiceCustomPropertySerializer (target, data)
{
    try  
    { target.push ( new CustomProperty (data['propname'], data['propvalue'], data['propeach']) );  
    } catch (ex) { }  
}

function InvoiceCustomDateSerializer (target, data)
{
    try  
    { target.push ( new CustomDate (data['FriendlyDate'], data['CustomDate']) );  
    } catch (ex) { }  
}

function ProcessInvoiceListResult (data)
{  
    var d=new dgrid();
    d.key="eivtable";
    d.showall = true;  
    d.columns = new Array ('Date','Event','Rate','Duration','Total'); 
    d.total = 0;
    d.hastotal = false;
    
    d.lastrowtransform = function ()
    {     
        var dmask = '';
        dmask += '<tr>';
        dmask += '  <td class="datarow" nowrap colspan="4" align="right">Total</td>'; 
        dmask += '  <td class="datarow" nowrap align="right" bgcolor="#FFFFCC"><b>' + number_format(this.total) + '</b></td>';
        dmask += '  <td class="datarow" nowrap width="100%">&nbsp;</td>';
        dmask += '</tr>'; 
        return dmask; 
    }
    d.header = Factory.write ({'color':'navy', 'text':'<b>&#171; Back to Events</b>','onclick':'HomePageEvent.invoke()','template':'controlLink'}); 
	
    d.transform = function (row)
    {   
        var tmp = null;
        var dmask = '';
        var rate = 0;

        
        for (var u in row.CustomProperties) 
	    { 
		    if (row.CustomProperties[u].name=='Rate') 
		    { 
			    rate=row.CustomProperties[u].value; 
			    break; 
		    } 
	    }   
	    
        if (row.InvoiceDisplayDates > 0)
        {
            for (var x=0;x<row.CustomDates.length;x++)
            {
                if (Math.pow(2,x) & row.InvoiceDisplayDates) 
                {
                    tmp = new InvoiceRow (row, rate, row.CustomDates[x].value, this.id, Math.pow(2,x));
                    dmask += tmp.draw ();  
                    d.total += tmp.total;
                }  
            }
        }
        else
        {
            tmp = new InvoiceRow (row, rate, row.EventDate, this.id, false); 
            dmask += tmp.draw (); 
            d.total += tmp.total;
        } 
        
        return dmask;
    }  
    
	d.rows = window.invoice.events;  
	
	
    document.title = window.invoice.events.length + ' events';
    
	d.cellpadding= 2; 
    return d.draw(); 
}

function TotalRow ( )
{
    this.istotal = true;
}

function InvoiceRow (row, rate, date, grid, subkey)
{
    this.row = row;
    this.rate = rate;
    this.date = date;
    this.grid = grid;
    this.total = 0;
    
    this.draw = function ()
    {
		var custom = null;
        var ar, nm
        this.total  = 0;
        var Factory = window.templates
        var dmask   = '';
        var sub     = this.row.Duration * this.rate;  
        
        var oncheck;
        if (subkey)
        {
            oncheck = '  window.invoice.clone (' + this.row.ID + ', ' + subkey + '); ';
        }  
        else
        {
            oncheck  = '  if (confirm(\'Remove item from invoice?\')) ';
            oncheck += '  { window.invoice.remove (' + this.row.ID + ');Controller.Reload(); } ';
            oncheck += ' else this.checked=true;';
        }
        
        var object =  { 'oncheck' : oncheck, 
                        'template' : 'invoice_event_row', 
                        'style' : subkey ? "color:#666" : (this.row.CustomDates.length > 0 ? 'font-weight:bold' : 'color:black;text-decoration:none'), 
                        'onclick' : this.row.CustomDates.length > 0 ? "AttachInvoiceDateForm ('" + this.row.ID + "', '" + this.grid + "')" : "", 
                        'id' : 'iv-' + this.row.ID + '', 
                        'linktext' : dateInput ( this.date ), 
                        'eventname' : this.row.EventName, 
                        'rate' : number_format( this.rate ), 
                        'duration' : number_format( this.row.Duration ), 
                        'sub' : number_format( sub )
                      };
                    
        dmask += Factory.write (object); 
        
        this.total += sub; 
        for (var k in this.row.CustomProperties)
        {
            custom=this.row.CustomProperties[k];
			if (custom.each && parseInt( custom.each ) > 0 && custom.value && typeof (custom.value) != 'undefined' && custom.value != 'undefined')
            {
                ar = custom.value.split (',')
                nm = GetCustomPropertyByName(custom.name);
                if (nm)
                {
                    for (var v in ar)
                    {
                        object =  { 'template' : 'invoice_property_row', 
                                    'L1' : nm.type_name=='number' ? '&nbsp;' : custom.name, 
                                    'L2' : nm.type_name=='number' ? custom.name : ar[v], 
                                    'L3' : number_format ( custom.each ), 
                                    'L4' : nm.type_name=='number' ? ar[v] : '&nbsp;', 
                                    'L5' : number_format( nm.type_name=='number' ? ( custom.each * ar[v] ) : custom.each )
                                  };
                      
                        dmask += Factory.write (object); 
                        this.total += parseInt( object.L5 );
                    }
                }
            }
        }
        
        return dmask;
    }
}
 

function ProcessResetEventInvoiceDate ()
{  
    if (window.http.readyState==ReadyState.DONE)   
    {    
	    HideTimeSelector(); 
        ReloadLastListRequest ();
    }  
}

function AttachInvoiceDateForm(id)
{
    registerTemplate ("InvoiceDateLine", InvoiceDateLine);   
	HideTimeSelector(); 
    var row = window.invoice.find (id);
    if (!row) return false;
	var dt=document.getElementById ('iv-' + id);
    if (!dt) return false;
	var ds=document.getElementById ('ddateselect'); 
    if (!ds) return false;
	var tb=document.getElementById ('dceivtable'); 
    if (!tb) return false; 
     
    var object = {'content' : '', 'footer' : '&nbsp;' + row.CustomDates.length + ' dates', 'template' : 'dropdown_menu'};
    for (var x=0;x<row.CustomDates.length;x++)
    {
        object.content += Factory.write ({ 'template' : 'InvoiceDateLine', 
                                             'id' : id, 
                                             'title' : row.CustomDates[x].title, 
                                             'value' : Math.pow(2,x), 
                                             'color' : Math.pow(2,x) & row.InvoiceDisplayDates ? 'black' : 'blue'
                                        });
    }   
     
    
	ds.innerHTML        = Factory.write (object); 
	ds.style.display    = 'inline'; 
	ds.style.left       = docx (dt);
	ds.style.top        = docy (dt) + dt.offsetHeight - tb.scrollTop;
} 


function number_format (x)
{
    if (isNaN(x)) x=0;
    var dec=Math.round(100 * (x-Math.floor(x)));
    if (dec < 10) dec = '0' + dec;
    return Math.floor(x) + '.' + dec;
}

var InvoiceDateLine = '';
InvoiceDateLine += '  <tr>';
InvoiceDateLine += '    <td><a style="color:(color)" ';
InvoiceDateLine += '    href="javascript:void(0)" onclick="window.invoice.clone ((id), (value));"';
InvoiceDateLine += '    >(title)</a></td>'; 
InvoiceDateLine += '  </tr>';  